package com.shwread.android.ui.dialog;

import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hzdracom.epubreader.consts.EpubConst;
import com.hzdracom.epubreader.db.BookInfoDbAdapter;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw.activity.MainActivity;
import com.shwread.android.qysw10000182.R;

import java.io.IOException;

import logic.bean.BookBean;
import logic.beanenum.BookStatus_Enum;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.BookDao;
import logic.dao.BookMarksDao;
import logic.db.DBHelper;
import logic.util.FileUtil;
import logic.util.Util;


public class DeleteShelfBookDialog
        extends
        Dialog
{
//	private TextView vContent;
	private TextView vOK;
	private TextView vCancel;
	private CheckBox cbDeleteFile;
	
	private MainActivity activity;
	private BookBean bookMarkBean;
	
	public DeleteShelfBookDialog(MainActivity activity,BookBean bookMarkBean) {
		super(activity, R.style.common_dialog);
		this.activity = activity;
		this.bookMarkBean=bookMarkBean;
		findViews();
		setListeners();
		setCanceledOnTouchOutside(true);
		getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		getWindow().setGravity(Gravity.BOTTOM);
	}
	
	private void findViews() {
		View view = LayoutInflater.from(activity).inflate(R.layout.shelf_delete_dialog, null);
		
		vCancel = (TextView) view.findViewById(R.id.shelf_delete_cancel);
		vOK = (TextView) view.findViewById(R.id.shelf_delete_ok);
//		vContent = (TextView) view.findViewById(R.id.shelf_delete_content);
		cbDeleteFile = (CheckBox) view.findViewById(R.id.shelf_delete_local_file);
		
		cbDeleteFile.setVisibility(bookMarkBean.status == BookStatus_Enum.LOCAL.getValue() ? View.VISIBLE : View.GONE);
		setContentView(view);
	}
	
	private void setListeners() {
		ClickListener l = new ClickListener();
		
		vOK.setOnClickListener(l);
		vCancel.setOnClickListener(l);
		
	}
	
	void clickOk() {
		if(bookMarkBean.book_type == BookBean.BOOK_TYPE_EPUB3){
			BookInfoDbAdapter adapter = new BookInfoDbAdapter(activity,DBHelper.getInstance(activity));
			adapter.open();
			adapter.removeTask(bookMarkBean.book_id,Const.phone_number);
			adapter.close();
			deleteEpubFile();
			BookDao.getInstance(activity).delReadingBook(bookMarkBean);
		}else{
			
			if( bookMarkBean.status != BookStatus_Enum.LOCAL.getValue() || 
					(bookMarkBean.status == BookStatus_Enum.LOCAL.getValue() && cbDeleteFile.isChecked()) ){
				deleteFile();
			}
			bookMarkBean.syncStatus = BookBean.DELETE;
			if(Util.isEmpty(bookMarkBean.account)) bookMarkBean.account = Const.phone_number;
			BookDao.getInstance(activity).updateSyncBook(bookMarkBean);
			BookMarksDao.getInstance(activity).deleteBook(bookMarkBean.book_id, Const.phone_number);
		}
		BaseSimpleActivity.clearNotification(activity, DefaultConsts.DOWNLOAD_NOTIFICATION_FLAG);
		activity.getUIHandler().sendEmptyMessage(DefaultConsts.DELETE_BOOKSHELF_BOOKS_OK);
//		new DelShelfBookAction(activity).start(bookMarkBean);
		dismiss();
	}
	
	private void deleteFile() {
		final long id = bookMarkBean.book_id;
	    if(id == -1L){ //本地导入文件
	    	if(Util.isNotEmpty(bookMarkBean.path) && FileUtil.exists(bookMarkBean.path)) {
	    		FileUtil.delFile(bookMarkBean.path);
	    	}
	    }else{ 
	    	String directoryPath = FileUtil.OnlinePath(String.valueOf(bookMarkBean.book_id));
	    	if( bookMarkBean.status == BookStatus_Enum.DOWNLOAD.getValue()){
	    		final String filePath = FileUtil.DownloadPath() + bookMarkBean.book_id + ".ceb";
		    	if(FileUtil.exists(filePath)){
		    		FileUtil.delFile(filePath);
		    	}
		    	directoryPath = FileUtil.DownloadPath() + bookMarkBean.book_id;
	    	}
	    	if(FileUtil.exists(directoryPath)){
	    		try
                {
	                FileUtil.deleteFolderFile(directoryPath, true);
                }
                catch (IOException e)
                {
	                e.printStackTrace();
                }
	    	}
	    }
    }

	void clickCancel() {
		dismiss();
	}
	
	private class ClickListener
	        implements
	        View.OnClickListener
	{
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
				case R.id.shelf_delete_ok:
					clickOk();
					break;
				case R.id.shelf_delete_cancel:
					clickCancel();
					break;
			}
		}
	}
	
	private void deleteEpubFile(){
		final String epubPath = FileUtil.DownloadPath() + bookMarkBean.book_id + EpubConst.EPUB_SUFFIX;
		final String cssCachePath = EpubConst.EPUB_CSS_LOCAL_PATH + bookMarkBean.book_id + "/" ;
		final String imageCachePath = EpubConst.EPUB_IMG_LOCAL_PATH + bookMarkBean.book_id + "/" ;
		try
		{
			FileUtil.deleteFolderFile(epubPath, false);
			FileUtil.deleteFolderFile(cssCachePath, true);
			FileUtil.deleteFolderFile(imageCachePath, true);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
